import os

import matplotlib.pyplot as plt
import numpy as np
from dateutil.utils import today
from numpy import sort
from openpyxl import Workbook, load_workbook
# y轴百分比、小数处理包
from matplotlib import ticker
from adjustText import adjust_text

# 显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']

wb = Workbook()
wb = load_workbook('直分图表数据py.xlsx', data_only=True)
sheet = wb['Sheet1']

DATE = [str(sheet['BE3'].value)[5:10], str(sheet['BA3'].value)[5:10], str(sheet['AW3'].value)[5:10],
        str(sheet['AS3'].value)[5:10], str(sheet['AO3'].value)[5:10], str(sheet['AK3'].value)[5:10],
        str(sheet['AG3'].value)[5:10], str(sheet['AC3'].value)[5:10], str(sheet['Y3'].value)[5:10],
        str(sheet['U3'].value)[5:10], str(sheet['Q3'].value)[5:10], str(sheet['M3'].value)[5:10],
        str(sheet['I3'].value)[5:10], str(sheet['E3'].value)[5:10]]

ZF_RATE = [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],
           [], [], [], []]

for i in range(0, 31):
    ZF_RATE[i] = [sheet['BG' + str(i + 33)].value, sheet['BC' + str(i + 33)].value, sheet['AY' + str(i + 33)].value,
                  sheet['AU' + str(i + 33)].value,
                  sheet['AQ' + str(i + 33)].value, sheet['AM' + str(i + 33)].value, sheet['AI' + str(i + 33)].value,
                  sheet['AE' + str(i + 33)].value, sheet['AA' + str(i + 33)].value,
                  sheet['W' + str(i + 33)].value, sheet['S' + str(i + 33)].value, sheet['O' + str(i + 33)].value,
                  sheet['K' + str(i + 33)].value, sheet['G' + str(i + 33)].value]

y = []
for i in range(0, 31):
    y.append(np.array(ZF_RATE[i]))
x1 = np.array(DATE)  # x_日期
fig = plt.figure(figsize=(16, 9))
plt.suptitle(str(sheet['E3'].value)[5:10][0:2]+'月'+str(sheet['E3'].value)[5:10][3:5]+'日'+'分拨直分占比曲线（一）', fontsize=18)
plt.axis('off')

# 子图1
ax1 = fig.add_subplot(221)
plt.plot(x1, y[0], 'blue', label='上海')
plt.plot(x1, y[1], 'red', label='浦东')
plt.plot(x1, y[2], 'teal', label='奉贤')
plt.plot(x1, y[3], 'green', label='昆山')
plt.plot(x1, y[4], 'brown', label='杭州')
plt.plot(x1, y[5], 'gray', label='嘉兴')
plt.plot(x1, y[6], 'orange', label='萧绍')
plt.plot(x1, y[7], 'purple', label='宁波')
ax1.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=4, frameon=False)  # 设置图例
lim_y1 = [sort(y[0])[13], sort(y[1])[13], sort(y[2])[13], sort(y[3])[13], sort(y[4])[13], sort(y[5])[13],
          sort(y[6])[13], sort(y[7])[13]]
lim_y1.sort()
plt.ylim(0, lim_y1[7] + 0.15)  # y轴取值范围
# plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('华东(8/31)', fontsize=14)
ax1.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y[i][13], '%.2f%%' % (y[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(0, 8)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )
# 子图2
ax2 = fig.add_subplot(222)
plt.plot(x1, y[8], 'blue', label='义乌')
plt.plot(x1, y[9], 'red', label='台州')
plt.plot(x1, y[10], 'teal', label='温州')
plt.plot(x1, y[11], 'green', label='南京')
plt.plot(x1, y[12], 'brown', label='淮安')
plt.plot(x1, y[13], 'gray', label='盐城')
plt.plot(x1, y[14], 'orange', label='泰州')
plt.plot(x1, y[15], 'purple', label='徐州')
ax2.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=4, frameon=False)  # 设置图例
lim_y1 = [sort(y[8])[13], sort(y[9])[13], sort(y[10])[13], sort(y[11])[13], sort(y[12])[13], sort(y[13])[13],
          sort(y[14])[13], sort(y[15])[13]]
lim_y1.sort()
plt.ylim(0, lim_y1[7] + 0.15)  # y轴取值范围
# plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('华东(16/31)', fontsize=14)
ax2.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y[i][13], '%.2f%%' % (y[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(8, 16)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )

# 子图3
ax3 = fig.add_subplot(223)
plt.plot(x1, y[16], 'blue', label='苏州')
plt.plot(x1, y[17], 'red', label='无锡')
plt.plot(x1, y[18], 'teal', label='南通')
plt.plot(x1, y[19], 'green', label='常州')
plt.plot(x1, y[20], 'brown', label='合肥')
plt.plot(x1, y[21], 'gray', label='蚌埠')
plt.plot(x1, y[22], 'orange', label='阜阳')
plt.plot(x1, y[23], 'purple', label='芜湖')
ax3.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=4, frameon=False)  # 设置图例
lim_y1 = [sort(y[16])[13], sort(y[17])[13], sort(y[18])[13], sort(y[19])[13], sort(y[20])[13], sort(y[21])[13],
          sort(y[22])[13], sort(y[23])[13]]
lim_y1.sort()
plt.ylim(0, lim_y1[7] + 0.15)  # y轴取值范围
plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('华东(24/31)', fontsize=14)
ax3.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y[i][13], '%.2f%%' % (y[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(16, 24)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )

# 子图4
ax4 = fig.add_subplot(224)
plt.plot(x1, y[24], 'blue', label='黄山')
plt.plot(x1, y[25], 'red', label='济南')
plt.plot(x1, y[26], 'teal', label='烟台')
plt.plot(x1, y[27], 'green', label='潍坊')
plt.plot(x1, y[28], 'brown', label='青岛')
plt.plot(x1, y[29], 'purple', label='临沂')
plt.plot(x1, y[30], 'orange', label='济宁')
ax4.legend(loc='upper center', bbox_transform=ax1.transAxes, ncol=4, frameon=False)  # 设置图例
lim_y1 = [sort(y[24])[13], sort(y[25])[13], sort(y[26])[13], sort(y[27])[13], sort(y[28])[13], sort(y[29])[13],
          sort(y[30])[13]]
lim_y1.sort()
plt.ylim(0, lim_y1[6] + 0.15)  # y轴取值范围
plt.xlabel('日期')
plt.ylabel('直分占比')
plt.title('华东(31/31)', fontsize=14)
ax4.yaxis.set_major_formatter(ticker.PercentFormatter(xmax=1, decimals=1))
texts = [plt.text(x1[13], y[i][13], '%.2f%%' % (y[i][13] * 100), fontsize=10, verticalalignment='bottom',
                  horizontalalignment='center') for i in range(24, 31)]
# adjust_text(texts,
#             arrowprops=dict(
#                 arrowstyle='-',  # 箭头样式
#                 lw=0.5,  # 线宽
#                 color='black')  # 箭头颜色
#             )

plt.show()
# os.mkdir('D:\\Desktop\\直分图表\\直分率\\2023'+str(today())[5:7] + str(today())[8:10])
# print('新建文件夹：D:\\Desktop\\直分图表\\直分率\\2023'+str(today())[5:7] + str(today())[8:10])
# plt.savefig(fname='D:\\Desktop\\直分图表\\直分率\\2023'+str(today())[5:7] + str(today())[8:10]+'\\03-华东'+str(today())[5:7]+str(today())[8:10]+'.png')
# print('03华东'+str(today())[5:7]+str(today())[8:10]+'.png'+'已保存')
# os.system('python 直分曲线（华南-西南）.py')
